/*-----------------------------------------------------------------------------
|                               File description                              |
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
 * @file      : template.h
 * @author    : TODO:YourName (TODO:yourEmail@xx.com)
 * @brief     : TODO: brief description
 * @version   : v0.0.1
 * @date      : 20xx.xx.xx
 * @note      : Encoding UTF-8 | 1 tab = 4 spaces | 1 line <= 80 characters
 * @license   : TODO:See www.runoob.com/w3cnote/open-source-license.html
 * Copyright (C) 2024 by TODO:YourName. All rights reserved.
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
|                                 Update note                                 |
-------------------------------------------------------------------------------
 * ------------   ---------------   ------------------------------------------
 *     Date            Author                          Note                   
 * ------------   ---------------   ------------------------------------------
 * 20xx.xx.xx     TODO:YourName     Create file
 *---------------------------------------------------------------------------*/

// FIXME: replace "TEMPLATE" and "template" with your module name
#ifndef TEMPLATE_H__
#define TEMPLATE_H__

/*-----------------------------------------------------------------------------
|                                Includes begin                               |
-----------------------------------------------------------------------------*/
#include <stdint.h>

/*-----------------------------------------------------------------------------
|                                Includes end                                 |
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
|                                Declare begin                                |
-----------------------------------------------------------------------------*/
#ifdef  __cplusplus
    extern "C" {
#endif /* __cplusplus */

// TODO:add your micro define here
#define TEMPLATE_INITED_MAGIC               0x5A
#define TEMPLATE_BUFFER_SIZE                128U

typedef enum template_status
{
    TEMPLATE_STATUS_OK = 0,         /* operate ok        */
    TEMPLATE_STATUS_ERROR,          /* operate error     */
    TEMPLATE_STATUS_INVALID_PARAM,  /* invalid parameter */
    TEMPLATE_STATUS_INITED,         /* already inited    */
    TEMPLATE_STATUS_NOT_INITED,     /* not inited        */
    TEMPLATE_STATUS_RESOURCE_ERROR, /* resource error    */
} template_status_t;



// TODO:add your external dependency here
typedef struct template_ex_port
{
    template_status_t (*pf_malloc)(uint16_t size, void **pp);
    void              (*pf_free)(void *p);
    template_status_t (*pf_write)(uint8_t *data, uint16_t len);
    template_status_t (*pf_read)(uint8_t *data, uint16_t len);
    template_status_t (*pf_ctrl)(uint32_t cmd, void *param);
} template_ex_port_t;
/* check if the external port is valid */
#define __CHECK_TEMPLATE_EX_PORT(p) ((p)            != NULL && \
                                     (p)->pf_malloc != NULL && \
                                     (p)->pf_free   != NULL && \
                                     (p)->pf_write  != NULL && \
                                     (p)->pf_read   != NULL && \
                                     (p)->pf_ctrl   != NULL    )

// TODO:add your object here  (maintain right or left alignment)
typedef struct template
{
    uint8_t                        is_init;
    struct template                  *next; // use struct template as list node
    const template_ex_port_t       *p_port;
    uint8_t                      *p_buffer;
} template_t;

/*-----------------------------------------------------------------------------
|                                 Declare end                                 |
-----------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------------
|                                  API begin                                  |
-----------------------------------------------------------------------------*/
/**
 * @brief constructor for template
 * @param template_t template handle
 * @param template_ex_port_t port handle
 * @retval template_status_t OK:TEMPLATE_STATUS_OK
 */
template_status_t template_constructor(template_t *pTemplate,
                                       const template_ex_port_t *pPort);
/**
 * @brief constructor for template
 * @param template_t template handle
 * @retval template_status_t OK:TEMPLATE_STATUS_OK
 */
template_status_t template_destructor(template_t *pTemplate);

template_status_t template_write(template_t *pTemplate,
                                 uint8_t         *data,
                                 uint16_t         len);
template_status_t template_read(template_t  *pTemplate,
                                 uint8_t         *data,
                                 uint16_t         len);
template_status_t template_ctrl(template_t  *pTemplate,
                                 uint32_t          cmd, 
                                 void          *param);

/*-----------------------------------------------------------------------------
|                                  API end                                    |
-----------------------------------------------------------------------------*/

#ifdef __cplusplus
	} /* extern "C" */
#endif /* __cplusplus */
#endif /* TEMPLATE_H__ */
